<?php
namespace app\route;
use think\Route;
use think\Config;
use think\DB;
use think\Request;


$taglib_pre_load = config('template')['taglib_pre_load'];
$taglib_pre_load = $taglib_pre_load . ',app\\article\\Tag';
$template = config('template');
$template['taglib_pre_load'] = $taglib_pre_load;
config('template',$template);
$request = Request::instance();
try {
    $articleSettingInfo = db('ArticlesSetting')->select();
    if ($articleSettingInfo) {
        foreach ($articleSettingInfo as $k => $v) {
            if ($v['key'] == 'data') {
                $articleSettingInfo[$v['key']] = $v['val'] ? json_decode($v['val'],true) : [];
            } else {
                $articleSettingInfo[$v['key']] = $v['val'];
            }
        }
    }
    config('articleSettingInfo',$articleSettingInfo);

    if (!strpos($request->url(),'Api')) {
        Route::rule(['/article/user/publish/?id=:id'=>['article/ArticleUser/publish',[],[]]]);
        Route::rule('/article/user/publish/','article/ArticleUser/publish');
        Route::rule('/article/user/articleList/','article/ArticleUser/articleList');
        Route::rule(['/article/index_<page>' => ['article/Article/index',['ext'=>'html'],['id'=>'[a-zA-Z0-9_-]+']]]);
        $categoryList = model('app\\article\\model\\ArticlesCategory')->getCategory();
        if ($categoryList) {
            foreach ($categoryList as $key => $value) {
                Route::rule([$value['pageRule'] => ['article/Article/index?id=' . $value["id"] . '&cid=' . $value["id"],[],[]]]);
                Route::rule([str_replace('.html','',$value['detailRule']).'_<page>' => ['article/ArticleDetail/index',['ext'=>'html'],['id'=>'[a-zA-Z0-9_-]+']]]);
                Route::rule([str_replace('.html','',$value['detailRule']) => ['article/ArticleDetail/index?cid=' . $value['id'],['__url__' => $value['detail__url__']],[]]]);
                if (strpos($value['rule'],'.html') !== false) {
                    Route::rule([str_replace('.html','',$value['rule']) => ['article/Article/index?id=' . $value["id"] . '&cid=' . $value["id"],['ext'=>'html'],[]]]);
                } else {
                    Route::rule([$value['rule'] . 'index'=> ['article/Article/index?id=' . $value["id"] . '&cid=' . $value["id"],['ext'=>'html'],[]]]);
                    Route::rule([$value['rule'] => ['article/Article/index?id=' . $value["id"] . '&cid=' . $value["id"],[],[]]]);
                }

            }
        }
        Route::rule(['/article/:id' => ['article/ArticleDetail/index',['ext'=>'html'],[],[]]]);
        Route::rule('article','article/Article/index');
        Route::rule('article/ArticleComponent/:act','article/ArticleComponent/:act');
        
        Route::rule(['baiduSmartApp.txt' => ['article/ArticleSitemap/baiduSmartApp',['ext'=>'txt'],[],[]]]);
    }
    if (strpos($request->url(),'Api') !== false) {
        Route::rule('article/:ctr/:act' ,'article/:ctr/:act'); 
    }
} catch (\Exception $e) {
    
}